home *** CD-ROM | disk | FTP | other *** search
/ Deutsche Edition 1 / Deutsche Edition 1.iso / amok / amok_lha / amok15.lha / Seafarers_Manual / Source / RaisePower2.mod < prev    next >
Text File  |  1993-08-15  |  1KB  |  61 lines

  1. MODULE RaisePower2;   (* Raise x to y power (y>=0) using local variables *)
  2.  
  3.   (* From the book "Modula-2  A Seafarer's Manual and Shipyard Guide" *)
  4.   (* Page 102   adapted "Amiga M2Modula-2"   08 Mar 1988 *)
  5.  
  6. FROM InOut IMPORT WriteLn,
  7.                   WriteString,
  8.                   ReadCard;
  9. FROM RealInOut IMPORT WriteReal,
  10.                       ReadReal;
  11.                       
  12. VAR
  13.   z,            (* accumulates result *)
  14.   x : REAL;        (* base from keyboard *)
  15.   y : CARDINAL;        (* exponent from keyboard *)
  16.   
  17. PROCEDURE Getxy;    (* get x & y from keyboard *)
  18.   BEGIN
  19.     WriteLn;
  20.     WriteString ("Enter x: ");
  21.     ReadReal (x);
  22.     WriteLn;
  23.     WriteString ("Enter y: ");
  24.     ReadCard (y);
  25.   END Getxy;
  26.   
  27. PROCEDURE Power;    (* raise x to y power *)
  28.   VAR
  29.     e : CARDINAL;    (* holds exponent *)
  30.     t : REAL;        (* intermediate result *)
  31.   BEGIN
  32.     e := y;        (* initialize exponent *)
  33.     t := x;        (* initialize intermediate result *)
  34.     z := 1.0;        (* initialize result *)
  35.     WHILE (e # 0) DO
  36.       WHILE (NOT ODD(e)) DO
  37.         t := t * t;
  38.         e := e DIV 2;
  39.       END;   (* WHILE NOT *)
  40.       z := z * t;
  41.       DEC (e);
  42.     END;   (* WHILE e *)
  43.   END Power;
  44.    
  45. PROCEDURE DisplayAnswer;
  46.   BEGIN
  47.     WriteLn;
  48.     WriteString ("x to y power = ");
  49.     WriteReal (z,10,2);
  50.     WriteLn;
  51.   END DisplayAnswer;
  52.  
  53. BEGIN            (* MODULE RaisePower *)
  54.   WriteLn;
  55.   WriteString ("Calculating x to y power");
  56.   Getxy;
  57.   Power;
  58.   DisplayAnswer;
  59.   
  60. END RaisePower2.
  61.